<template>
    
    <!-- 组件的模板 -->
    <!-- <div class="weui-cells__title">表单填写要求</div>
    <div class="weui-cells">
        <a class="weui-cell weui-cell_access" href="javascript:;">
            <div class="weui-cell__bd">
                <p>表单字段格式设计</p>
            </div>
            <div class="weui-cell__ft">
            </div>
        </a>
    </div> -->
    <!-- <div class="weui-cells__title">表单填写要求</div> -->
    <!-- <div class="weui-cells margin_top_12">
        <a class="weui-cell weui-cell_access" href="dmsaas://KaoqinAddGroup">
            <div class="weui-cell__bd">
                <p>
                    <span class="rgb_153 margin_rignt_18">参与小组</span>
                    <span class="rgb_51" v-if="setupFormInfo.group_list.length > 0">已选择{{setupFormInfo.group_list.length}}个小组</span>
                    <span class="rgb_187" v-else>请选择</span>
                </p>
            </div>
            <div class="weui-cell__ft"></div>
        </a>
        <a class="weui-cell weui-cell_access" href="dmsaas://KaoqinAddMember">
            <div class="weui-cell__bd">
                <p>
                    <span class="rgb_153 margin_rignt_18">参与成员</span>
                    <span class="rgb_51" v-if="setupFormInfo.user_list.length > 0">已选择{{setupFormInfo.user_list.length}}个成员</span>
                    <span class="rgb_187" v-else>请选择</span>
                </p>
            </div>
            <div class="weui-cell__ft"></div>
        </a>        
    </div> -->

    <div class="weui-cells">
        <a class="weui-cell_access block" href="dmsaas://KaoqinAddGroup">
            <div class="weui-cell">
                <div class="weui-cell__bd">
                    <p>参与小组<span class="tip">(新增人员自动加入)</span></p>
                </div>
                <div class="weui-cell__ft" v-if="setupFormInfo.group_list.length > 0">{{setupFormInfo.group_list.length}}个小组</div>
                <div class="weui-cell__ft" v-else></div>
            </div>
            <div class="add-team">
                <template v-if="setupFormInfo.group_list.length > 0">
                    <span class="team" v-for="item in setupFormInfo.group_name_list">{{item}}</span>
                </template>
                <span class="button"></span>
            </div>
        </a>
        <a class="weui-cell weui-cell_access" href="dmsaas://KaoqinAddMember">
            <div class="weui-cell__bd">
                <p>其他参与人员</p>
            </div>
            <div class="weui-cell__ft" v-if="setupFormInfo.user_list.length > 0">{{setupFormInfo.user_list.length}}人</div>
            <div class="weui-cell__ft" v-else></div>
        </a>
    </div> 


    <div class="weui-cells margin_top_12">
        <div class="weui-cell weui-cell_switch">
            <div class="weui-cell__bd rgb_51">表单是否可以多次提交</div>
            <div class="weui-cell__ft">
                <input class="weui-switch" type="checkbox" v-model="setupFormInfo.repeat_submit"/>
            </div>
        </div>
        <div class="weui-cell weui-cell_switch">
            <div class="weui-cell__bd rgb_51">表单提交后是否可以删除</div>
            <div class="weui-cell__ft">
                <input class="weui-switch" type="checkbox" v-model="setupFormInfo.is_can_delete"/>
            </div>
        </div>
    </div>
    <!-- <div class="weui-cells__title">提醒与通知</div> -->
    <div class="weui-cells margin_top_12">
        <a class="weui-cell weui-cell_access" href="javascript:;" @click="gotoRemind">
            <div class="weui-cell__bd rgb_51">
                <p>提醒设置</p>
            </div>
            <div class="weui-cell__ft">{{remind_info}}
            </div>
        </a>
        <!-- <div class="weui-cell weui-cell_switch">
            <div class="weui-cell__bd">表单提交时通知我</div>
            <div class="weui-cell__ft">
                <input class="weui-switch" type="checkbox" v-model="setupFormInfo.is_send_notice"/>
            </div>
        </div> -->
    </div>
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import widget from 'gapi/widget.js'
    import clientUi from 'gapi/clientUi.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    import * as util from 'js/util.js'
    import commonPage from 'clientApi/commonPage.js'
    import cache from 'gapi/cache.js'
    
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                setupFormInfo: {
                    form_id: 0,
                    team_id: 0,
                    project_id: 0,
                    group_list: [],
                    user_list: [],
                    end_time: '',
                    // is_send_notice: 0, //提交表单是否发送通知0-否1-是
                    is_can_delete: 1, //表单提交后是否可以删除 0-否 1-是
                    repeat_submit: 1, //是否可以多次提交 0-否 1-是
                    remind_type: 2, //1-特定日期 2-固定时间段
                    remind_date: [],
                    remind_time: [],
                    type: 2,//edit 2 , create 1
                    lastPageType: 1,//上一页来源：1-表单 2-签到
                    group_name_list: [],//小组名
                },
                encodeStr: '',
                remind_info: '不提醒'
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.setupFormInfo.type = util.getUrddataString('type')
                // widget.toast('type: '+this.setupFormInfo.type)
                commonPage.updateTitleBar({
                    backAction: 'checkChange',    // 点击返回按钮的动作（即回调函数名称）
                    // leftDisplay: false,            // 是否显示左按钮
                    // leftText: '',                // 左按钮文案
                    leftAction: 'checkChange',    // 点击左按钮的动作（即回调函数名称）
                    rightDisplay: true,            // 是否显示右按钮
                    rightText: '提交',            // 右按钮文案
                    rightAction: 'saveResume'    // 点击右按钮的文案（即回调函数名称）
                })
                window.saveResume = () => {
                    //表单设置上传到服务器
                    this.setupFormInfo.is_can_delete = this.setupFormInfo.is_can_delete ? 1 : 0
                    this.setupFormInfo.repeat_submit = this.setupFormInfo.repeat_submit ? 1 : 0
                    console.log("表单设置提交数据"+JSON.stringify(this.setupFormInfo))
                    commonPage.loadPageStatus('loading')
                    util.ajax({
                        url: '/form/setting',
                        type: 'POST',
                        data: this.setupFormInfo,
                        timeout: 10000,
                        success: (data) => {
                            commonPage.loadPageStatus('loadSuccess')
                            // success
                            // alert(JSON.stringify(data))
                            if(data.errno == 0){
                                //埋点
                                let statisticsLog_data = {
                                    event_id: '表单设置-提交',
                                    // event_label: data.event_label,(预留，现在不用传此参数)
                                    event_param: {
                                        ca_from: 'submit',
                                        project_id: this.setupFormInfo.project_id,
                                    }
                                }
                                // alert(JSON.stringify(statisticsLog_data))
                                KCApiApp.statisticsLog(statisticsLog_data)

                                //是否可以多次提交埋点
                                if(this.setupFormInfo.repeat_submit == 1){
                                    //埋点
                                    let statisticsLog_data = {
                                        event_id: '表单设置-可多次提交',
                                        // event_label: data.event_label,(预留，现在不用传此参数)
                                        event_param: {
                                            ca_from: 'morethanonce',
                                            project_id: this.setupFormInfo.project_id,
                                        }
                                    }
                                    // alert(JSON.stringify(statisticsLog_data))
                                    KCApiApp.statisticsLog(statisticsLog_data)
                                }else{
                                    //埋点
                                    let statisticsLog_data = {
                                        event_id: '表单设置-不可多次提交',
                                        // event_label: data.event_label,(预留，现在不用传此参数)
                                        event_param: {
                                            ca_from: 'onlyonce',
                                            project_id: this.setupFormInfo.project_id,
                                        }
                                    }
                                    // alert(JSON.stringify(statisticsLog_data))
                                    KCApiApp.statisticsLog(statisticsLog_data)
                                }
                                //是否可以多次删除
                                if(this.setupFormInfo.is_can_delete == 1){
                                    //埋点
                                    let statisticsLog_data = {
                                        event_id: '表单设置-可删除',
                                        // event_label: data.event_label,(预留，现在不用传此参数)
                                        event_param: {
                                            ca_from: 'candelete',
                                            project_id: this.setupFormInfo.project_id,
                                        }
                                    }
                                    // alert(JSON.stringify(statisticsLog_data))
                                    KCApiApp.statisticsLog(statisticsLog_data)
                                }else{
                                    //埋点
                                    let statisticsLog_data = {
                                        event_id: '表单设置-不可删除',
                                        // event_label: data.event_label,(预留，现在不用传此参数)
                                        event_param: {
                                            ca_from: 'cannotdelete',
                                            project_id: this.setupFormInfo.project_id,
                                        }
                                    }
                                    // alert(JSON.stringify(statisticsLog_data))
                                    KCApiApp.statisticsLog(statisticsLog_data)
                                }
                                //返回表单设置列表页
                                widget.toast('更改设置成功')
                                location.href = `dmsaas://SetupForm`
                            }else if(data.errno == 22002){
                                widget.toast('请选择填写人员')
                            }else{
                                // widget.toast('服务器异常')
                                widget.toast(data.errmsg)
                            }
                        },
                        error: (xhr, status) => {
                            commonPage.loadPageStatus('loadSuccess')
                            // alert(JSON.stringify(xhr))
                            // alert(JSON.stringify(status))
                            // error
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            commonPage.loadPageStatus('loadSuccess')
                            // 无网络的回调
                            widget.toast('网络连接失败，请检查网络')
                        }
                    })
                      
                }
                window.checkChange = () => {
                    // clientUi.showDialog('提醒', '请设置需要填写表单的人员和表单到期时间!', (status) => {
                        // alert(status.status)
                        // if(status.status === '0'){
                            //返回表单设置列表页
                            location.href = `dmsaas://SetupForm`
                        // }
                    // }, '继续设置', '暂不设置')
                }
                window.loadData = (data) => {
                    // alert(JSON.stringify(data))
                    if(data){
                        if(data.key === 'participantGroup'){
                            if(data.value.length > 0){
                                this.setupFormInfo.group_list = data.value
                                this.setupFormInfo.group_name_list = data.name
                            }else{
                                this.setupFormInfo.group_list = []
                                this.setupFormInfo.group_name_list = []
                            }
                            cache.setMemoryCache('participantGroup', data.value)
                        }else if(data.key === 'participantMember'){
                            if(data.value.length > 0){
                                this.setupFormInfo.user_list = data.value

                            }else{
                                this.setupFormInfo.user_list = []
                            }
                            cache.setMemoryCache('participantMember', data.value)
                        }else{
                            // this.setupFormInfo.remind_type = data.remind_type;
                            this.setupFormInfo.remind_date = data.remind_date;
                            this.setupFormInfo.remind_time = data.remind_time;
                            if(this.setupFormInfo.remind_time.length == 0){
                                this.remind_info = '不提醒'
                            }else if(this.setupFormInfo.remind_time.length == 1){
                                this.remind_info = this.setupFormInfo.remind_time
                            }else if(this.setupFormInfo.remind_time.length == 2){
                                this.remind_info = this.setupFormInfo.remind_time[0] + '、' + this.setupFormInfo.remind_time[1] 
                            }else if(this.setupFormInfo.remind_time.length > 2){
                                this.remind_info = this.setupFormInfo.remind_time[0] + '、' + this.setupFormInfo.remind_time[1] + '...'
                            }
                            // alert(JSON.stringify(this.setupFormInfo))
                        }
                    }
                }

                
                // if(util.OS()=='ios'){
                    commonPage.getWebViewID((data)=>{
                        cache.setMemoryCache('webViewID',{'webViewID':data})
                        // window.location.href="dmsaas://KaoqinAddGroup"
                    })
                // }
            },
            // setEndTime (){
            //     let date = new Date()
            //     let timestr = date.getTime()
            //     let today = util.getLocalTime(timestr, 'yyyy-MM-dd')
            //     KCApiApp.showDatePicker(today, '2019-11-10', this.setupFormInfo.end_time || today, (data) => {
            //         // alert(JSON.stringify(data.SelectedDate))
            //         // this.setupFormInfo.remind_time.push(data.SelectedDate)
            //         this.setupFormInfo.end_time = data.SelectedDate
            //     })
            // },
            gotoRemind (){
                //埋点
                let statisticsLog_data = {
                    event_id: '表单设置-表单提醒设置',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'formpushsetting',
                        project_id: this.setupFormInfo.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                let setupFormInfo = JSON.stringify(this.setupFormInfo)
                let encodeStr = encodeURIComponent(`setupFormInfo=${setupFormInfo}`)
                location.href = `dmsaas://SetupFormRemind?urdData=${encodeStr}`
            },
            //重新请求设置接口
            reloadUpdate (){
                //根据form_id获取设置
                this.setupFormInfo.form_id = util.getUrddataString('form_id');
                // alert(this.setupFormInfo.form_id)
                cache.getCache('user_info', (data) => {
                    this.setupFormInfo.team_id = data.team_id
                    this.setupFormInfo.project_id = data.project_id
                    util.ajax({
                        url: '/form/getsetting',
                        type: 'POST',
                        data: {
                            team_id: data.team_id,
                            project_id: data.project_id,
                            form_id: this.setupFormInfo.form_id
                        },
                        timeout: 10000,
                        success: (data) => {
                            // success
                            if(data.errno == 0){
                                console.log("获取表单配置"+JSON.stringify(data))
                                this.setupFormInfo.group_list = data.data.group_list
                                this.setupFormInfo.group_name_list = data.data.group_name_list
                                this.setupFormInfo.user_list = data.data.user_list
                                this.setupFormInfo.is_can_delete = data.data.is_can_delete
                                this.setupFormInfo.repeat_submit = data.data.repeat_submit
                                // this.setupFormInfo.remind_type = data.data.remind_type
                                this.setupFormInfo.remind_date = data.data.remind_date ? data.data.remind_date : []
                                this.setupFormInfo.remind_time = data.data.remind_time ? data.data.remind_time : []
                                if(this.setupFormInfo.remind_time.length == 0){
                                    this.remind_info = '不提醒'
                                }else if(this.setupFormInfo.remind_time.length == 1){
                                    this.remind_info = this.setupFormInfo.remind_time
                                }else if(this.setupFormInfo.remind_time.length == 2){
                                    this.remind_info = this.setupFormInfo.remind_time[0] + '、' + this.setupFormInfo.remind_time[1] 
                                }else if(this.setupFormInfo.remind_time.length > 2){
                                    this.remind_info = this.setupFormInfo.remind_time[0] + '、' + this.setupFormInfo.remind_time[1] + '...'
                                }
                                cache.setMemoryCache('participantGroup', data.data.group_list)
                                cache.setMemoryCache('participantMember', data.data.user_list)
                                cache.setMemoryCache('participantGroupname', data.data.group_name_list)
                            }else{
                                // widget.toast('服务器异常')
                                widget.toast(data.errmsg)
                            }
                            
                        },
                        error: (xhr, status) => {
                            // error
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            // 无网络的回调
                            widget.toast('网络连接失败，请检查网络')
                        }
                    })
                })
            }
        },
        created(){
            //根据form_id获取设置
            this.setupFormInfo.form_id = util.getUrddataString('form_id');
            // alert(this.setupFormInfo.form_id)
            cache.getCache('user_info', (data) => {
                this.setupFormInfo.team_id = data.team_id
                this.setupFormInfo.project_id = data.project_id
                util.ajax({
                    url: '/form/getsetting',
                    type: 'POST',
                    data: {
                        team_id: data.team_id,
                        project_id: data.project_id,
                        form_id: this.setupFormInfo.form_id
                    },
                    timeout: 10000,
                    success: (data) => {
                        // success
                        if(data.errno == 0){
                            console.log("获取表单配置"+JSON.stringify(data))
                            this.setupFormInfo.group_list = data.data.group_list
                            this.setupFormInfo.group_name_list = data.data.group_name_list
                            this.setupFormInfo.user_list = data.data.user_list
                            this.setupFormInfo.is_can_delete = data.data.is_can_delete
                            this.setupFormInfo.repeat_submit = data.data.repeat_submit
                            // this.setupFormInfo.remind_type = data.data.remind_type
                            this.setupFormInfo.remind_date = data.data.remind_date ? data.data.remind_date : []
                            this.setupFormInfo.remind_time = data.data.remind_time ? data.data.remind_time : []
                            if(this.setupFormInfo.remind_time.length == 0){
                                this.remind_info = '不提醒'
                            }else if(this.setupFormInfo.remind_time.length == 1){
                                this.remind_info = this.setupFormInfo.remind_time
                            }else if(this.setupFormInfo.remind_time.length == 2){
                                this.remind_info = this.setupFormInfo.remind_time[0] + '、' + this.setupFormInfo.remind_time[1] 
                            }else if(this.setupFormInfo.remind_time.length > 2){
                                this.remind_info = this.setupFormInfo.remind_time[0] + '、' + this.setupFormInfo.remind_time[1] + '...'
                            }
                            cache.setMemoryCache('participantGroup', data.data.group_list)
                            cache.setMemoryCache('participantMember', data.data.user_list)
                            cache.setMemoryCache('participantGroupname', data.data.group_name_list)
                        }else if(data.errno == 22007){
                            this.reloadUpdate()
                        }else{
                            // widget.toast('服务器异常')
                            widget.toast(data.errmsg)
                        }
                        
                    },
                    error: (xhr, status) => {
                        // error
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            })
            this.init();
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/attendance-set.css"></style>
<style type="text/css">
    body{
        background: #f2f2f2;
    }
    /*.line-len{
        max-width: 204px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }*/
    div.margin_top_12{
        margin-top:12px;
        /*margin-bottom: 16px;*/
    }
    a.weui-cell{
        padding:15px 12px;
    }
    .weui-cell .rgb_51{
        color:rgb(51, 51, 51);
    }
    .weui-cell .rgb_153{
        color:rgb(153, 153, 153);
        /*flex: none;
        width: 82px;*/
    }
    .weui-cell .margin_rignt_18{
        margin-right: 18px;
    }
    .weui-cell .rgb_187{
        color:rgb(187, 187, 187);
    }
</style>


